<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 放置结构内容 -->
      <el-card>
        <el-tabs>
            <el-tab-pane label="角色管理">
              <!-- 左侧的内容 -->
              <el-row style="height: 60px">
                <el-button icon="el-icon-plus" type="primary" size="small" @click="showDialog = true">
                  新增角色
                </el-button>
              </el-row>
              <!-- 表格 -->
              <el-table border="" :data="list">
                <el-table-column label="序号" width="120" align="center" type="index"></el-table-column>
                <el-table-column label="名称" width="240" align="center" prop="name"></el-table-column>
                <el-table-column label="描述" align="center" prop="description"></el-table-column>
                <el-table-column label="操作" align="center">
                  <template slot-scope="{ row }">
                    <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                    <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                    <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!-- 放置分页组件 -->
              <el-row type="flex" justify="center" align="middle" style="height: 60px">
                <el-pagination layout="prev,pager,next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
                ></el-pagination>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="公司信息">
              <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重写审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
              >
              </el-alert>
              <el-form label-width="120px" style="margin-top: 50px">
                <el-form-item label="公司名称">
                  <el-input v-model="formDate.name" disabled style="width: 400px"></el-input>
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input v-model="formDate.companyAddress" disabled style="width: 400px"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="formDate.mailbox" disabled style="width: 400px"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                  <el-input v-model="formDate.remarks" disabled style="width: 400px" type="textarea" :rows="3"></el-input>
                </el-form-item>
              </el-form>
            </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 放置一个弹层组件 -->
    <el-dialog :title="dialogTitle" :visible="showDialog" @close="Cancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="Cancel">取消</el-button>
          <el-button size="small" type="primary" @click="sumbit" >确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹层 -->
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <!-- 权限是一棵树 -->
      <el-tree
      ref="permTree"
      :data="permData"
      :props="defaultProps"
      :show-checkbox="true"
      :check-strictly="true"
      :default-expand-all="true"
      :default-checked-keys="selectCheck"
      node-key="id"
      ></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOk">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { tranListToTreeDate } from '@/utils'
import { getPermissionList } from '@/api/permisson'
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 5,
        total: 0 // 记录总数
      },
      formDate: {
        // 公司信息
      },
      showDialog: false,
      roleForm: {

      },
      rules: {
        name: [ { required: true, message: '角色名称不能为空', trigger: 'blur'} ]
      },
      showPermDialog: false, // 控制分配权限
      defaultProps: {
        label: 'name'
      },
      permData: [], // 接收权限树形数据
      selectCheck: [], // 定义一个数组来接收已选择的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    dialogTitle () {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created () {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  methods: {
    async getRoleList () {
      const { total, rows} = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage (newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页面赋值给当前的最新页码
      this.getRoleList() // 初始化数据
    },
    async getCompanyInfo () {
      this.formDate = await getCompanyInfo(this.companyId)
    },
    // 删除角色
    async deleteRole (id) {
      try {
        await this.$confirm('确认删除该角色吗?')
        // 点击了确定后才能进入到下方
        await deleteRole(id) // 调用删除角色接口
        this.getRoleList() // 重新加载数据
        this.$message.success('删除角色成功')
      } catch (e) {
        console.log(e);
      }
    },
    async editRole (id) {
      // 为了不出现闪烁的问题 先获取数据再弹出层
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    // 提交数据
    async sumbit () {
      try {
        await this.$refs.roleForm.validate()
        // 只有校验通过的情况下，才会执行await下方的内容
        // roleForm这个对象有id就是编辑没有id就是新增
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          // 新增业务
          await addRole(this.roleForm)
        }
        // 重新初始数据
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (e) {
        console.log(e);
      }
    },
    // 重置
    Cancel () {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 分配权限
    async assignPerm(id) {
      this.permData = tranListToTreeDate(await getPermissionList(), '0') // 转换list到树形数据
      this.roleId = id
      const { permIds } = await getRoleDetail(id) // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds // 将当前角色所拥有的权限is赋值
      this.showPermDialog = true
    },
    async btnPermOk () {
      // 调用 el-tree的方法
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    btnPermCancel () {
      this.selectCheck = [] // 重置数据
      this.showPermDialog = false
    }
  },
}
</script>

<style>

</style>
